home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 422_02 / dosutil / size.c < prev    next >
C/C++ Source or Header  |  1994-03-20  |  2KB  |  80 lines

  1. /*
  2.  * Reads one or more files, and displays the number of lines, and the
  3.  * total size of the file in bytes.
  4.  *
  5.  * Demonstrates setting up and using a two byte counter, which is used
  6.  * to keep track of the total number of characters. Up to 655350000
  7.  * characters can be counted.
  8.  *
  9.  * Copyright 1991-1994 Dave Dunfield
  10.  * All rights reserved.
  11.  *
  12.  * Permission granted for personal (non-commercial) use only.
  13.  *
  14.  * Compile command: cc size -fop
  15.  */
  16. #include <stdio.h>
  17.  
  18. unsigned tlines = 0, tcharh = 0, tcharl = 0;
  19.  
  20. /*
  21.  * Convert a double word number to an ASCII string
  22.  */
  23. char *dtoa(high, low)
  24.     unsigned high, low;
  25. {
  26.     static char dtoabuf[10];
  27.  
  28.     if(high)
  29.         sprintf(dtoabuf,"%u%04u", high, low);
  30.     else
  31.         sprintf(dtoabuf,"%u", low);
  32.  
  33.     return dtoabuf;
  34. }
  35.  
  36. /*
  37.  * Main program, determine size of files
  38.  */
  39. main(argc, argv)
  40.     int argc;
  41.     char *argv[];
  42. {
  43.     unsigned i;
  44.     FILE *fp;
  45.  
  46.     if(argc == 1)
  47.         dosize(stdin,"<stdin>");
  48.     else {
  49.         for(i=1; i < argc; ++i)
  50.             if(fp = fopen(argv[i],"rvb")) {
  51.                 dosize(fp,argv[i]);
  52.                 fclose(fp); } }
  53.     if(argc > 2)
  54.         printf("\nTotal of %s characters, %u lines.\n", dtoa(tcharh, tcharl), tlines);
  55. }
  56.  
  57. /*
  58.  * Process an individual file & determine its size
  59.  */
  60. dosize(fp, name)
  61.     FILE *fp;
  62.     char *name;
  63. {
  64.     unsigned lines, charh, charl, chr;
  65.  
  66.     lines = charh = charl = 0;
  67.     while((chr = getc(fp)) != -1) {
  68.         if(++charl >= 10000) {
  69.             ++charh;
  70.             charl -= 10000; }
  71.         if(chr == '\n')
  72.             ++lines; }
  73.     tlines += lines;
  74.     if((tcharl += charl) >= 10000) {
  75.         ++tcharh;
  76.         tcharl -= 10000; }
  77.     tcharh += charh;
  78.     printf("%12s characters,%5u lines in file: '%s'\n", dtoa(charh, charl), lines, name);
  79. }
  80.